﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Dost.Khcn.Data.EntityModel;
using Dost.Khcn.Data.Interfaces;

namespace Dost.Khcn.Data.Implements
{
    public class LinhVucDao : ILinhVucDao
    {
        #region LinhVuc

        public List<LinhVuc> GetAll(int? nhomLinhVucId)
        {
            using (var db = new NLKHCNDataEntities())
            {
                var query = from item in db.LinhVucs select item;
                if (nhomLinhVucId.HasValue && nhomLinhVucId > 0)
                {
                    query = query.Where(x => x.NhomLinhVucId == nhomLinhVucId);
                }
                return query.OrderBy(x => x.Ma).ThenBy(x => x.Ten).ToList();
            }
        }

        public List<LinhVuc> GetLinhVucs(int startIndex, int pageSize, string sorting, string name, int nhomLinhVucId, string code, out int totalRecords)
        {
            using (var db = new NLKHCNDataEntities())
            {
                var query = from item in db.LinhVucs
                            select item;

                // filter
                if (!string.IsNullOrEmpty(name))
                {
                    query = query.Where(x => x.Ten.Contains(name));
                }
                if (!string.IsNullOrEmpty(code))
                {
                    query = query.Where(x => x.Ma.Contains(code));
                }
                if (nhomLinhVucId > 0)
                {
                    query = query.Where(x => x.NhomLinhVucId == nhomLinhVucId);
                }

                totalRecords = query.Count();

                return pageSize > 0
                           ? query.OrderBy(sorting).Skip(startIndex).Take(pageSize).ToList() //Paging
                           : query.ToList(); //No paging
            }
        }

        public LinhVuc AddLinhVuc(LinhVuc linhVuc)
        {
            using (var db = new NLKHCNDataEntities())
            {
                //CodeIsExisted
                var count = (from o in db.LinhVucs
                             where (o.Ma == linhVuc.Ma)
                             select o.Id).Count();
                if (count > 0)
                    throw new ApplicationException("CodeIsExisted");

                //NameIsExisted
                count = (from o in db.LinhVucs
                         where (o.Ten == linhVuc.Ten)
                               && ((!o.NhomLinhVucId.HasValue && !linhVuc.NhomLinhVucId.HasValue)
                                   || (o.NhomLinhVucId.Value == linhVuc.NhomLinhVucId.Value))
                         select o.Id).Count();

                if (count > 0)
                    throw new ApplicationException("NameIsExisted");

                db.LinhVucs.Add(linhVuc);
                db.SaveChanges();
            }

            return linhVuc;
        }

        public LinhVuc GetById(int id)
        {
            using (var db = new NLKHCNDataEntities())
            {
                return db.LinhVucs.FirstOrDefault(x => x.Id == id);
            }
        }

        public void UpdateLinhVuc(LinhVuc linhVuc)
        {
            using (var db = new NLKHCNDataEntities())
            {
                //CodeIsExisted
                var count = (from o in db.LinhVucs
                             where (o.Ma == linhVuc.Ma && o.Id != linhVuc.Id)
                             select o.Id).Count();
                if (count > 0)
                    throw new ApplicationException("CodeIsExisted");

                //NameIsExisted
                count = (from o in db.LinhVucs
                         where (o.Ten == linhVuc.Ten) && (o.Id != linhVuc.Id)
                               && ((!o.NhomLinhVucId.HasValue && !linhVuc.NhomLinhVucId.HasValue)
                                   || (o.NhomLinhVucId.Value == linhVuc.NhomLinhVucId.Value))
                         select o.Id).Count();

                if (count > 0)
                    throw new ApplicationException("NameIsExisted");

                db.LinhVucs.Attach(linhVuc);
                db.Entry(linhVuc).State = EntityState.Modified;
                db.SaveChanges();
            }
        }

        public void DeleteLinhVuc(int id)
        {
            if (id <= 0) return;
            using (var db = new NLKHCNDataEntities())
            {
                db.DeleteLinhVuc(id);
            }
        }

        #endregion

        #region NhomLinhVuc

        public List<NhomLinhVuc> GetAllNhomLinhVucs()
        {
            using (var db = new NLKHCNDataEntities())
            {
                return db.NhomLinhVucs.OrderBy(x => x.Ma).ToList();
            }
        }

        public List<NhomLinhVuc> GetNhomLinhVucs(int startIndex, int pageSize, string sorting, string name, string code, out int totalRecords)
        {
            using (var db = new NLKHCNDataEntities())
            {
                var query = from item in db.NhomLinhVucs
                            select item;

                // filter
                if (!string.IsNullOrEmpty(name))
                {
                    query = query.Where(x => x.Ten.Contains(name));
                }

                if (!string.IsNullOrEmpty(code))
                {
                    query = query.Where(x => x.Ma.Contains(code));
                }

                totalRecords = query.Count();

                return pageSize > 0
                           ? query.OrderBy(sorting).Skip(startIndex).Take(pageSize).ToList() //Paging
                           : query.ToList(); //No paging
            }
        }

        public NhomLinhVuc AddNhomLinhVuc(NhomLinhVuc nhomLinhVuc)
        {
            using (var db = new NLKHCNDataEntities())
            {
                //CodeIsExisted
                var count = (from o in db.NhomLinhVucs
                             where (o.Ma == nhomLinhVuc.Ma)
                             select o.Id).Count();

                if (count > 0)
                    throw new ApplicationException("CodeIsExisted");

                //NameIsExisted
                count = (from o in db.NhomLinhVucs
                         where (o.Ten == nhomLinhVuc.Ten)
                         select o.Id).Count();

                if (count > 0)
                    throw new ApplicationException("NameIsExisted");

                db.NhomLinhVucs.Add(nhomLinhVuc);
                db.SaveChanges();
            }

            return nhomLinhVuc;
        }

        public void UpdateNhomLinhVuc(NhomLinhVuc nhomLinhVuc)
        {
            using (var db = new NLKHCNDataEntities())
            {
                //CodeIsExisted
                var count = (from o in db.NhomLinhVucs
                             where (o.Ma == nhomLinhVuc.Ma && o.Id != nhomLinhVuc.Id)
                             select o.Id).Count();

                if (count > 0)
                    throw new ApplicationException("CodeIsExisted");

                //NameIsExisted
                count = (from o in db.NhomLinhVucs
                         where (o.Ten == nhomLinhVuc.Ten && o.Id != nhomLinhVuc.Id)
                         select o.Id).Count();

                if (count > 0)
                    throw new ApplicationException("NameIsExisted");

                db.NhomLinhVucs.Attach(nhomLinhVuc);
                db.Entry(nhomLinhVuc).State = EntityState.Modified;
                db.SaveChanges();
            }
        }

        public void DeleteNhomLinhVuc(int id)
        {
            if (id <= 0) return;
            using (var db = new NLKHCNDataEntities())
            {
                db.DeleteNhomLinhVuc(id);
            }
        }

        #endregion
    }
}
